<?php

namespace Biz\model\package;

use Doraemon\pockets\datebase\BaseDbModel;

class PackageOrderLog extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db';
    const _CONFIG_SELECT_ = 'db_activity';
    const _DATABASE_ = 'hhz_activity';

    const _TABLE_NAME = 'new_user_package_order_log';

    // 1、发货日志 2、批量发货 3、手动触发物流该状态 4、订单创建失败 5、修改地址 6支付失败
    const LOG_TYPE_1 = 1;
    const LOG_TYPE_2 = 2;
    const LOG_TYPE_3 = 3;
    const LOG_TYPE_4 = 4;
    const LOG_TYPE_5 = 5;
    const LOG_TYPE_6 = 6;


    public static function getById($id, $field = "*")
    {
        return self::get($field, ['id' => $id]);
    }

    public static function getByOrderNo($orderNo, $field = "*")
    {
        return self::get($field, ['order_no' => $orderNo]);
    }

    /**
     * @param $orderNo
     * @param $data
     * @return bool|int|mixed|string
     */
    public static function createOrUpdate($orderNo, $data)
    {
        $one = self::getByOrderNo($orderNo);
        if (empty($one)) {
            return self::add($data);
        } else {
            return self::edit($orderNo, $data);
        }
    }

    public static function add($data)
    {
        $data['addtime'] = time();
        if (self::insert($data)->rowCount()) {
            return self::id();
        }
        return false;
    }

    public static function edit($orderNo, $data)
    {
        $data['edittime'] = time();
        if (self::update($data, ['order_no' => $orderNo])) {
            return $orderNo;
        }
        return false;
    }

    public static function editById($id, $data)
    {
        $data['edittime'] = time();
        if (self::update($data, ['id' => $id])) {
            return $id;
        }
        return false;
    }

    public static function getList($where, $limit, $field = '*')
    {
        if (!empty($limit)) {
            $where['LIMIT'] = $limit;
        }
        return self::select($field, $where) ?: [];
    }


}